package com.library.uiframework.widget.actionbar;

import java.util.ArrayList;
import java.util.List;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.library.uiframework.R;
import com.library.uiframework.utils.ContextUtils;
import com.library.uiframework.utils.DisplayUtils;
import com.library.uiframework.widget.icon.IconTextView;

/**
 * @author hongkai.qian
 * @version 1.0.0
 * @since 15/10/21
 */
public class ActionBarLayout extends ViewGroup {

    private View mBackgroundView;
    private View mForegroundView;

    private LinearLayout mLeftActionContainer;
    private FrameLayout mCenterActionContainer;
    private LinearLayout mRightActionContainer;

    private Animation mAnimationProgress;
    private List<Action> mLeftActionList = new ArrayList<>();
    private List<Action> mRightActionList = new ArrayList<>();

    /**
     * 纯文本形式
     */
    public static final int MODE_TEXT = 0;
    /**
     * 图 - 文本 -字体
     * <p>
     * 左 R.id.niv_icon 默认隐藏 NetworkImageView
     * 中 同 MODE_TEXT
     * 右 R.id.itv_icon  IconTextView
     */
    public static final int MODE_IMAGE_TEXT = 1;
    /**
     * tab
     */
    //public static final int MODE_TAB = 2;
    /**
     * 搜索
     */
    public static final int MODE_SEARCH = 3;
//    /**
//     * 自定义
//     */
//    public static final int MODE_CUSTOM = 9;


    private TextView mTvTitle;
    private View mSearchInputLayout;

    public ActionBarLayout(Context context) {
        super(context);
        init(context, null, 0);
    }

    public ActionBarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public ActionBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private LayoutInflater requestLayoutInflater(Context context) {
        return LayoutInflater.from(context);
    }

    public void setActionBarBackgroundResource(@ColorRes int color) {
        mBackgroundView.setBackgroundResource(color);
    }

    public void setActionBarBackground(@ColorInt int color) {
        mBackgroundView.setBackgroundColor(color);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        setClipToPadding(false);
        if (isInEditMode()) {
            ContextUtils.setContext(context);
        }
        mBackgroundView = new View(context);
        mBackgroundView.setBackgroundResource(R.color.ykp_c7);
        addView(mBackgroundView);
        mCenterActionContainer = new FrameLayout(context);
        mCenterActionContainer.setMinimumHeight(getResources().getDimensionPixelOffset(R.dimen.action_bar_height));
        int minWidth = DisplayUtils.dp2px(20);
        mLeftActionContainer = new LinearLayout(context);
        mLeftActionContainer.setOrientation(LinearLayout.HORIZONTAL);
        mLeftActionContainer.setMinimumWidth(minWidth);
        mRightActionContainer = new LinearLayout(context);
        mRightActionContainer.setOrientation(LinearLayout.HORIZONTAL);
        mRightActionContainer.setMinimumWidth(minWidth);

        int padding = DisplayUtils.dp2px(1);
        mLeftActionContainer.setPadding(padding, 0, 0, 0);
        mRightActionContainer.setPadding(0, 0, padding, 0);

        addView(mLeftActionContainer);
        addView(mCenterActionContainer);
        addView(mRightActionContainer);

//        mCenterActionContainer.setBackgroundColor(Color.MAGENTA);
        // mTvTitle.setTextSize(16);

//        enableTitle();
//        mTvTitle.setIconVisibility(View.GONE);
//        mTvTitle.getTextView().setEllipsize(TextUtils.TruncateAt.END);

        mForegroundView = new View(context);
        addView(mForegroundView);
        mForegroundView.setVisibility(View.GONE);
    }

    /**
     * 自定义中间位置添加view
     *
     * @param view view
     */
    public void setCenterView(View view) {
        mCenterActionContainer.removeAllViews();
//        view.setBackgroundColor(0x40000000);
        mCenterActionContainer.addView(view);
    }

    public void setMode(int mode) {
        View centerView;
        if (mode == MODE_TEXT) {
            mTvTitle = new TextView(getContext());
            Resources resources = getContext().getResources();
            mTvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(R.dimen.ykp_f5));
            mTvTitle.setTextColor(resources.getColor(R.color.ykp_c1));
            mTvTitle.setTypeface(null, Typeface.BOLD);
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            layoutParams.gravity = Gravity.CENTER;
            mTvTitle.setLayoutParams(layoutParams);
            mTvTitle.setGravity(Gravity.CENTER);
            mTvTitle.setSingleLine();
            mTvTitle.setEllipsize(TextUtils.TruncateAt.END);
            centerView = mTvTitle;
        } else {
            int resId;
            switch (mode) {
                //case MODE_TAB:
                //    break;

                case MODE_SEARCH:
                    resId = R.layout.uiframework_action_bar_search;
                    break;

                case MODE_IMAGE_TEXT:
                    resId = R.layout.uiframework_action_bar_image_text;
                    break;

                default:
                    return;
            }
            centerView = LayoutInflater.from(getContext()).inflate(resId, mCenterActionContainer, false);
            mTvTitle = (TextView) centerView.findViewById(R.id.tv_title);
            mSearchInputLayout = centerView.findViewById(R.id.search_input_layout);
        }

        setCenterView(centerView);
    }

    /**
     * @return 中间view
     */
    public FrameLayout getCenterActionContainer() {
        return mCenterActionContainer;
    }

    /**
     * @return 前景view，用来添加前景色造成某些视觉效果
     */
    public View getForegroundView() {
        return mForegroundView;
    }

    /**
     * @return 背景渐变色view
     */
    public View getBackgroundView() {
        return mBackgroundView;
    }

    @Override
    public Drawable getBackground() {
        return new ColorDrawable(Color.RED);
    }

    public TextView getTitleView() {
        return mTvTitle;
    }

    public View getSearchInputLayout() {
        return mSearchInputLayout;
    }

    /**
     * hide all action.
     *
     * @param enable true if yes
     */
    public void hideAllAction(boolean enable) {
        mRightActionContainer.setVisibility(enable ? View.INVISIBLE : View.VISIBLE);
        mLeftActionContainer.setVisibility(enable ? View.INVISIBLE : View.VISIBLE);
    }

    /**
     * 设置标题文本
     *
     * @param text text
     */
    public void setTitle(CharSequence text) {
        if (mTvTitle != null) {
            mTvTitle.setText(text);
        }
    }

    /**
     * 设置标题文本
     *
     * @param title    title
     * @param subTitle subTitle
     */
    public void setTitle(String title, String subTitle) {
        if (mTvTitle != null) {
            if (TextUtils.isEmpty(title) && TextUtils.isEmpty(subTitle)) {
                mTvTitle.setText("");
                return;
            }
            SpannableString spanString = new SpannableString(title + subTitle);
            if (!TextUtils.isEmpty(subTitle)) {
                AbsoluteSizeSpan span = new AbsoluteSizeSpan(14, true);
                int start = !TextUtils.isEmpty(title) ? title.length() : 0;
                spanString.setSpan(span, start, start + subTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            }
            mTvTitle.setText(spanString);
        }
    }

    /**
     * 设置标题文本
     *
     * @param textResourceId text resource id
     */
    public void setTitle(int textResourceId) {
        if (mTvTitle != null) {
            mTvTitle.setText(textResourceId);
        }
    }

    /**
     * 设置标题颜色
     *
     * @param color color
     */
    public void setTitleColor(int color) {
        if (mTvTitle != null) {
            mTvTitle.setTextColor(color);
        }
    }

    /**
     * 设置标题颜色
     *
     * @param color color
     */
    public void setTitleColorRes(@ColorRes int color) {
        if (mTvTitle != null) {
            mTvTitle.setTextColor(mTvTitle.getResources().getColor(color));
        }
    }

    private void cancelProgressAnimation(View view) {
        if (view.getVisibility() == View.VISIBLE) {
            view.clearAnimation();
            view.setVisibility(View.GONE);
        }
    }

    private void startProgressAnimation(View view) {
        if (view.getVisibility() != View.VISIBLE) {
            view.setAnimation(getAnimationProgress());
            view.setVisibility(View.VISIBLE);
        }
    }

    private Animation getAnimationProgress() {
        if (mAnimationProgress == null) {
            mAnimationProgress = AnimationUtils.loadAnimation(getContext(), R.anim.uikit_unlimited_rotate);
        }
        return mAnimationProgress;
    }

    /**
     * @param alpha 浮动模式工具栏透明度 取值区间[0-1]
     */
    public void setFloatingModeAlpha(float alpha) {
        mBackgroundView.setAlpha(alpha);
        if (mTvTitle != null) {
            mTvTitle.setAlpha(alpha);
        }
        for (Action action : mLeftActionList) {
            action.setFloatingModeAlpha(alpha);
        }
        for (Action action : mRightActionList) {
            action.setFloatingModeAlpha(alpha);
        }

    }

    /**
     * @param alpha alpha
     * @return 使alpha取值有效
     */
    public static float validAlpha(float alpha) {
        if (alpha < 0) {
            alpha = 0;
        } else if (alpha > 1) {
            alpha = 1;
        }
        return alpha;
    }

    /**
     * 增加一个图片样式的动作按钮
     *
     * @param imageResourceId resource id
     * @return action
     */
    public Action addImageAction(int actionId, int imageResourceId) {
        Drawable drawable = getContext().getResources().getDrawable(imageResourceId);
        return addImageAction(actionId, drawable);
    }

    /**
     * 增加一个图片样式的动作按钮
     *
     * @param drawable resource drawable
     * @return action
     */
    public Action addImageAction(int actionId, Drawable drawable) {
        return addImageAction(actionId, drawable, null);
    }

    /**
     * 增加一个图片样式的动作按钮
     *
     * @param drawable   resource drawable
     * @param contentDes 按钮描述（专为自动化测试添加）
     * @return action
     */
    public Action addImageAction(int actionId, Drawable drawable, String contentDes) {
        Action headerActionVariable = addHeaderAction(actionId, contentDes);
        headerActionVariable.setType(Action.TYPE_IMAGE_BUTTON);
        headerActionVariable.setImage(drawable);
        return headerActionVariable;
    }


    /**
     * 增加一个字体图标文字样式的动作按钮
     *
     * @param iconTextResourceId resource id
     * @param contentDes         按钮描述（专为自动化测试添加）
     * @return action
     */
    public Action addIconTextAction(int actionId, int iconTextResourceId, String contentDes) {
        Action headerActionVariable = addHeaderAction(actionId, contentDes);
        headerActionVariable.setType(Action.TYPE_IMAGE_BUTTON);
        headerActionVariable.setIconText(iconTextResourceId);
        return headerActionVariable;
    }

    /**
     * 增加一个文字样式的动作按钮
     *
     * @param textResourceId resource id
     * @return action
     */
    public Action addTextAction(int actionId, int textResourceId) {
        Action headerActionVariable = addHeaderAction(actionId, null);
        headerActionVariable.setType(Action.TYPE_TEXT_BUTTON);
        headerActionVariable.setText(textResourceId);
        return headerActionVariable;
    }

    /**
     * 增加一个文字样式的动作按钮
     *
     * @param actionId actionId
     * @param text     text
     * @return action
     */
    public Action addTextAction(int actionId, String text) {
        Action headerActionVariable = addHeaderAction(actionId, null);
        headerActionVariable.setType(Action.TYPE_TEXT_BUTTON);
        headerActionVariable.setText(text);
        return headerActionVariable;
    }

    /**
     * 增加一个图片样式的动作按钮
     *
     * @param imageResourceId resource id
     * @return action
     */
    public Action addLeftImageAction(int actionId, int imageResourceId) {
        Drawable drawable = getContext().getResources().getDrawable(imageResourceId);
        return addLeftImageAction(actionId, drawable);
    }

    /**
     * 增加一个图片样式的动作按钮
     *
     * @param drawable resource drawable
     * @return action
     */
    public Action addLeftImageAction(int actionId, Drawable drawable) {
        return addLeftImageAction(actionId, drawable, null);
    }

    /**
     * 增加一个图片样式的动作按钮
     *
     * @param drawable   resource drawable
     * @param contentDes 按钮描述（专为自动化测试添加）
     * @return action
     */
    public Action addLeftImageAction(int actionId, Drawable drawable, String contentDes) {
        Action headerActionVariable = addLeftHeaderAction(actionId, contentDes);
        headerActionVariable.setType(Action.TYPE_IMAGE_BUTTON);
        headerActionVariable.setImage(drawable);
        return headerActionVariable;
    }


    /**
     * 增加一个字体图标文字样式的动作按钮
     *
     * @param iconTextResourceId resource id
     * @param contentDes         按钮描述（专为自动化测试添加）
     * @return action
     */
    public Action addLeftIconTextAction(int actionId, int iconTextResourceId, String contentDes) {
        Action headerActionVariable = addLeftHeaderAction(actionId, contentDes);
        headerActionVariable.setType(Action.TYPE_IMAGE_BUTTON);
        headerActionVariable.setIconText(iconTextResourceId);
        return headerActionVariable;
    }

    /**
     * 增加一个文字样式的动作按钮
     *
     * @param textResourceId resource id
     * @return action
     */
    public Action addLeftTextAction(int actionId, int textResourceId) {
        Action headerActionVariable = addLeftHeaderAction(actionId, null);
        headerActionVariable.setType(Action.TYPE_TEXT_BUTTON);
        headerActionVariable.setText(textResourceId);
        return headerActionVariable;
    }

    /**
     * 增加一个文字样式的动作按钮
     *
     * @param text text
     * @return action
     */
    public Action addLeftTextAction(int actionId, String text) {
        Action headerActionVariable = addLeftHeaderAction(actionId, null);
        headerActionVariable.setType(Action.TYPE_TEXT_BUTTON);
        headerActionVariable.setText(text);
        return headerActionVariable;
    }

    private Action addHeaderAction(int actionId, String contentDes) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.uiframework_action_bar_action, mRightActionContainer, false);
        Action headerActionVariable = new Action(actionId, view);
        if (contentDes != null) {
            view.findViewById(R.id.image_variable).setContentDescription(contentDes);
        }
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//        layoutParams.rightMargin = DisplayUtils.dp2px(12);
        mRightActionContainer.addView(view, layoutParams);
        mRightActionList.add(headerActionVariable);
        return headerActionVariable;
    }

    private Action addLeftHeaderAction(int actionId, String contentDes) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.uiframework_action_bar_action, mLeftActionContainer, false);
        Action headerActionVariable = new Action(actionId, view);
        if (contentDes != null) {
            view.findViewById(R.id.image_variable).setContentDescription(contentDes);
        }
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//        layoutParams.leftMargin = DisplayUtils.dp2px(12);
        mLeftActionContainer.addView(view, layoutParams);
        mLeftActionList.add(headerActionVariable);
        return headerActionVariable;
    }

    /**
     * 移除一个Action
     *
     * @param action action
     */
    public void removeAction(Action action) {
        if (mRightActionList.contains(action)) {
            mRightActionList.remove(action);
            mRightActionContainer.removeView(action.mActionView);
        } else {
            mLeftActionList.remove(action);
            mLeftActionContainer.removeView(action.mActionView);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        View bkgView = getChildAt(0);
        View leftLayout = getChildAt(1);
        View middleLayout = getChildAt(2);
        View rightLayout = getChildAt(3);
        View foreView = getChildAt(4);

        bkgView.layout(l, t, r, b);
        foreView.layout(l, t, r, b);

        int viewTop = t + getPaddingTop();
        int viewBottom = b - getPaddingBottom();

        leftLayout.layout(l, viewTop, l + leftLayout.getMeasuredWidth(), viewBottom);

        int left = l + leftLayout.getMeasuredWidth();
        middleLayout.layout(left, viewTop, left + middleLayout.getMeasuredWidth(), viewBottom);

        left += middleLayout.getMeasuredWidth();
        rightLayout.layout(left, viewTop, left + rightLayout.getMeasuredWidth(), viewBottom);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();
        if (childCount != 5) {
            throw new AssertionError("ActionBarLayout can only 5 child view");
        }
        View bkgView = getChildAt(0);
        View leftLayout = getChildAt(1);
        View middleLayout = getChildAt(2);
        View rightLayout = getChildAt(3);
        View foreView = getChildAt(4);

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int atMostWidth = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
        leftLayout.measure(atMostWidth, heightMeasureSpec);
        rightLayout.measure(atMostWidth, heightMeasureSpec);
        int leftWidth = leftLayout.getMeasuredWidth();
        int rightWidth = rightLayout.getMeasuredWidth();

        int actionWidth = Math.max(leftWidth, rightWidth);
        int actionExactlyWidth = MeasureSpec.makeMeasureSpec(actionWidth, MeasureSpec.EXACTLY);
        if (leftWidth != actionWidth) {
            leftLayout.measure(actionExactlyWidth, heightMeasureSpec);
        } else if (rightWidth != actionWidth) {
            rightLayout.measure(actionExactlyWidth, heightMeasureSpec);
        }
        int middleHeight;
        final LayoutParams layoutParams = middleLayout.getLayoutParams();
        if (layoutParams.height == LayoutParams.MATCH_PARENT || layoutParams.height == LayoutParams.WRAP_CONTENT) {
            middleHeight = heightMeasureSpec;
        } else {
            middleHeight = MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY);
        }
        middleLayout.measure(MeasureSpec.makeMeasureSpec(width - actionWidth - actionWidth, MeasureSpec.EXACTLY), middleHeight);
        int maxHeight = leftLayout.getMeasuredHeight();
        maxHeight = Math.max(maxHeight, middleLayout.getMeasuredHeight());
        maxHeight = Math.max(maxHeight, rightLayout.getMeasuredHeight());
        maxHeight += getPaddingTop() + getPaddingBottom();
//        maxHeight = Math.max(maxHeight, getMinimumHeight());
        int measuredHeight = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.EXACTLY);
        bkgView.measure(widthMeasureSpec, measuredHeight);
        foreView.measure(widthMeasureSpec, measuredHeight);
        setMeasuredDimension(widthMeasureSpec, measuredHeight);

//        LogUtils.e("ActionBarLayout", "onMeasure mode=%08X height=%d leftHeight=%d middleHeight=%d middleMode=%08X selfHeight=%d"
//                , MeasureSpec.getMode(heightMeasureSpec), MeasureSpec.getSize(heightMeasureSpec), leftLayout.getMeasuredHeight()
//            , middleLayout.getMeasuredHeight(), middleLayout.getMeasuredState(), getMeasuredHeight());
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        for (Action action : mRightActionList) {
            action.cancelRotation();
        }
    }

    /**
     * 动作条Action项
     */
    public final class Action {

        /**
         * 样式-只显示图片
         */
        public static final int TYPE_IMAGE_BUTTON = 0;
        /**
         * 样式-只显示文字
         */
        public static final int TYPE_TEXT_BUTTON = 1;
        private static final int ANIM_DURATION = 1500;
        private static final int TO_DEGREES = 360;
        private static final int MAX_BLUR_RADIUS = 25;

        private View mActionView;
        //        private View mActionShadow;
        private FrameLayout mRelativeActionVariable;
        private IconTextView mImageProgress;
        private TextView mTextVariable;
        private IconTextView mImageVariable;
        private OnActionClickListener mOnClickListener;
        private Object mTag;
        private boolean mEnable = true;
        private View mRedDotView;

        private int mActionId;
        private ObjectAnimator mAnimator;

        private Action(int actionId, View action) {
            mActionView = action;
            mActionId = actionId;
            mRelativeActionVariable = (FrameLayout) action.findViewById(R.id.relative_variable);
            mImageProgress = (IconTextView) action.findViewById(R.id.image_progress);
            mTextVariable = (TextView) action.findViewById(R.id.text_variable);
            mImageVariable = (IconTextView) action.findViewById(R.id.image_variable);
//            mActionShadow = action.findViewById(R.id.view_shadow);
//            mActionShadow.setVisibility(View.GONE);
            mImageProgress.setVisibility(View.INVISIBLE);
            mRelativeActionVariable.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnClickListener != null) {
                        mOnClickListener.onClick(mActionId, Action.this);
                    }
                }
            });
            mRedDotView = action.findViewById(R.id.action_bar_red_dot);
        }

        /**
         * startRotation
         */
        public void startRotation() {
            if (mAnimator != null) {
                mAnimator.cancel();
            }
            float value = mImageProgress.getRotation();
            mAnimator = ObjectAnimator.ofFloat(mImageProgress, "rotation", value, TO_DEGREES + value);
            mAnimator.setDuration(ANIM_DURATION);
            mAnimator.setInterpolator(new LinearInterpolator());
            mAnimator.setRepeatCount(ObjectAnimator.INFINITE);
            mAnimator.start();
        }

        /**
         * cancelRotation
         */
        public void cancelRotation() {
            if (mAnimator != null) {
                mAnimator.cancel();
            }
        }

        /**
         * 获取Context
         *
         * @return context
         */
        public Context getContext() {
            return mRelativeActionVariable.getContext();
        }

        /**
         * 设置是否开启此动作按钮
         *
         * @param enable true if yes
         */
        public void setEnable(boolean enable) {
            mEnable = enable;
            if (isVisible()) {
                mRelativeActionVariable.setEnabled(enable);
            }
        }

        public void setEnableWithDark(boolean enable) {
            mEnable = enable;
            if (isVisible()) {
                mRelativeActionVariable.setEnabled(enable);
            }

            mRelativeActionVariable.setAlpha(enable ? 1.0f : 0.3f);
        }

        /**
         * 隐藏按钮
         *
         * @param withPlace 如果仍然占据空间，设置为true
         */
        public void hide(boolean withPlace) {
            mRelativeActionVariable.setVisibility(withPlace ? View.INVISIBLE : View.GONE);
            mRelativeActionVariable.setEnabled(false);
        }

        /**
         * 隐藏按钮，并让出所占用的空间
         */
        public void hide() {
            hide(false);
        }

        /**
         * 显示按钮
         */
        public void show() {
            setVisible(true);
        }

        /**
         * 设置是否显示此动作按钮
         *
         * @param visible true if yes
         */
        public void setVisible(boolean visible) {
            mRelativeActionVariable.setVisibility(visible ? View.VISIBLE : View.GONE);
            mRelativeActionVariable.setEnabled(visible & mEnable);
        }

        /**
         * 是否可见
         *
         * @return true if yes
         */
        public boolean isVisible() {
            return mRelativeActionVariable.getVisibility() == View.VISIBLE;
        }

        /**
         * 是否开启
         *
         * @return true if yes
         */
        public boolean isEnable() {
            return mEnable;
        }

        /**
         * 设置动作按钮回调
         *
         * @param onActionClickListener listener
         */
        public void setOnActionClickListener(OnActionClickListener onActionClickListener) {
            mOnClickListener = onActionClickListener;
        }


        /**
         * @return Icon ImageVariable
         */
        public IconTextView getImageVariable() {
            return mImageVariable;
        }


        /**
         * 是否在进度动画中
         *
         * @return true if yes
         */
        public boolean inProgress() {
            return mImageProgress.getVisibility() == View.VISIBLE;
        }

        /**
         * 设置进度图标
         *
         * @param progressImage 进度图标
         */
        public void setProgressImage(Drawable progressImage) {
            if (null != progressImage) {
                mImageProgress.setImageDrawable(progressImage);
            }
        }

        /**
         * 设置进度图标
         *
         * @param resId text resource id
         */
        public void setProgressIconText(int resId) {
            if (0 != resId) {
                mImageProgress.setText(resId);
            }
        }

        /**
         * 设置进度图标
         *
         * @param colorStateList colorStateList
         */
        public void setProgressIconTextColor(ColorStateList colorStateList) {
            if (null != colorStateList) {
                mImageProgress.setTextColor(colorStateList);
            }
        }

        /**
         * 请求显示动画
         */
        public void requestProgress() {
            startProgressAnimation(mImageProgress);
            mRelativeActionVariable.setEnabled(false);
            mImageVariable.setVisibility(View.INVISIBLE);
            mTextVariable.setVisibility(View.INVISIBLE);
        }

        /**
         * 设置附带信息
         *
         * @param tag tag
         */
        public void setTag(Object tag) {
            mTag = tag;
        }

        /**
         * 获取附带信息
         *
         * @return tag
         */
        public Object getTag() {
            return mTag;
        }

        /**
         * 结束进度动画
         */
        public void cancelProgress() {
            cancelProgressAnimation(mImageProgress);
            mRelativeActionVariable.setEnabled(true);
            mImageVariable.setVisibility(View.VISIBLE);
            mTextVariable.setVisibility(View.VISIBLE);
        }

        /**
         * 设置类型
         *
         * @param type type
         */
        public void setType(int type) {
            switch (type) {
                case TYPE_TEXT_BUTTON:
                default:
                    mImageVariable.setVisibility(View.INVISIBLE);
                    mTextVariable.setVisibility(View.VISIBLE);
                    break;
                case TYPE_IMAGE_BUTTON:
                    mTextVariable.setVisibility(View.INVISIBLE);
                    mImageVariable.setVisibility(View.VISIBLE);
                    break;
            }
        }

        /**
         * 设置文字
         *
         * @param textResourceId resource id
         */
        public void setText(int textResourceId) {
            mTextVariable.setText(textResourceId);
        }

        /**
         * 设置文字
         *
         * @param text text
         */
        public void setText(CharSequence text) {
            mTextVariable.setText(text);
        }

        public void setTextColor(ColorStateList colorStateList) {
            mTextVariable.setTextColor(colorStateList);
        }

        public void setTextColor(int color) {
            mTextVariable.setTextColor(color);
        }

        /**
         * 设置图片
         *
         * @param imageResourceId resource id
         */
        public void setImage(int imageResourceId) {
            mImageVariable.setImageResource(imageResourceId);
        }

        /**
         * 设置图片
         *
         * @param imageDrawable drawable
         */
        public void setImage(Drawable imageDrawable) {
            mImageVariable.setImageDrawable(imageDrawable);
        }


        /**
         * @param txtResId text resource id
         */
        public void setIconText(int txtResId) {
            mImageVariable.setText(txtResId);
        }

        /**
         * @param colorStateList colorStateList
         */
        public void setIconTextColor(ColorStateList colorStateList) {
            mImageVariable.setTextColor(colorStateList);
        }

        public View getActionView() {
            return mActionView;
        }

        public FrameLayout getRelativeActionVariable() {
            return mRelativeActionVariable;
        }

        public IconTextView getImageProgress() {
            return mImageProgress;
        }

        public TextView getTextVariable() {
            return mTextVariable;
        }

        public OnActionClickListener getOnClickListener() {
            return mOnClickListener;
        }

        public void setOnClickListener(OnActionClickListener onClickListener) {
            mOnClickListener = onClickListener;
        }

        public int getActionId() {
            return mActionId;
        }

        /**
         * 设置是否显示红点
         *
         * @param show 是否显示红点
         */
        public void showRedDot(boolean show) {
            if (mRedDotView != null) {
                mRedDotView.setVisibility(show ? View.VISIBLE : View.GONE);
            }
        }

        /**
         * @param alpha 浮动模式工具栏透明度 取值区间[0-1]
         */
        public void setFloatingModeAlpha(float alpha) {
            int color = Color.GRAY;
            int shadowRadius = Math.min(alpha >= 0.5f ? 0 : DisplayUtils.dp2px(8), MAX_BLUR_RADIUS);
            if (mTextVariable.getVisibility() == View.VISIBLE) {
                mTextVariable.setShadowLayer(shadowRadius, 0, 0, color);
            }
            if (mImageVariable.getVisibility() == View.VISIBLE) {
                mImageVariable.setShadowLayer(shadowRadius, 0, 0, color);
            }
            if (mImageProgress.getVisibility() == View.VISIBLE) {
                mImageProgress.setShadowLayer(shadowRadius, 0, 0, color);
            }
//            mActionShadow.setVisibility(alpha >= 0.5f ? View.GONE : View.VISIBLE);
//            if (mActionShadow.getVisibility() != View.VISIBLE) {
//                mActionShadow.setVisibility(View.VISIBLE);
//            }
//            mActionShadow.setAlpha(1 - alpha);
        }
    }

    /**
     *
     */
    public interface OnActionClickListener {
        /**
         * on click
         *
         * @param actionId actionId 触发点击事件后,上层可知道点击的是哪个按钮
         * @param action   action action 也是告知点的哪个按钮
         */
        void onClick(int actionId, Action action);
    }

    /**
     *
     */
    public interface OnHeaderClickListener {
        /**
         * on click
         *
         * @param actionBarLayout actionBarLayout
         */
        void onClick(ActionBarLayout actionBarLayout);
    }
}
